var confirmIsProcess = false;
function popupShow(obj, tit, url, callback, isfull, width, height) {
	$(obj).attr("disabled",true);
	var _width  = "80%";
	var _height = "60%";
	
	if(parseInt(width) != 0 && typeof width != "undefined") {
		_width = width+"px";
	}
	if(parseInt(height) != 0 && typeof height != "undefined") {
		_height = height+"px";
	}
	var addLayer = layer.open({
		type: 2,
		title: tit,
		shadeClose: false,
		shade: [0.5 , '#000' , true],
		shift: 2,
		maxmin: true,
		area: [_width, _height],
		content: url,
		cancel: function(){
			$(obj).removeAttr("disabled");
			if(typeof callback == 'function') callback();
		},
	});
	if(isfull == 1) {
		layer.full(addLayer);
	} 
}
function delHtmlTag(str) {
	return str.replace(/<[^>]+>/g,"");
}

function errorTips(btnID, dom, msg) {
	layer.tips(msg, $(dom), {
		tips: [1, '#d9534f'],
		time: 4000
	});
	$(dom).focus();
	$("#"+btnID).removeAttr("disabled");
	return false;
}

function ajaxError(msg, callback) {
	layer.msg(msg, {icon: 2,shadeClose: true,shade:[0.5 , 'pink' , true]},function(){
		confirmIsProcess = false;
		(typeof callback == "function") && callback();
	});
}

function ajaxSuccess(msg, callback) {
	layer.msg(msg,{icon:1,shadeClose: true,shade:[0.5 , '#000' , true]},function(){
		confirmIsProcess = false;
		(typeof callback == "function") && callback();
	});
}

function ajaxConfirm(question, successFun, cancelFun){
	layer.confirm(question, {btn: ['确定', '取消'], icon: 3},
		function() {
			if(typeof successFun == "function") {
				if(!confirmIsProcess) {
					confirmIsProcess = true;
					successFun();
				}
			}
		},
		function() {
			if(typeof cancelFun == "function") {
				if(!confirmIsProcess) {
					confirmIsProcess = true;
					cancelFun();
				}
			}
		}
	);
}

function ajaxJSon(url, callback) {
	$.get(url, function(data) { 
		(typeof callback == "function") && callback(data);
	},'json');
}

function ajaxGet(url, obj, callback) {
	$(obj).attr("disabled",true);
	if(!$(obj).hasClass("isclick")) {
		$(obj).addClass("isclick");
		$.get(url, function(_) { 
			if (_.error) {
				ajaxError(_.msg);
				$(obj).removeAttr("disabled");
				$(obj).removeClass("isclick");
			} else {
				ajaxSuccess(_.msg, function(){
					$(obj).removeAttr("disabled");
					$(obj).removeClass("isclick");
					(typeof callback == "function") && callback(_);
				});
			}
		},'json');
	}
}

function ajaxPost(url, obj, data, callback) {
	$(obj).attr("disabled",true);
	if(!$(obj).hasClass("isclick")) {
		$(obj).addClass("isclick");
		$.post(url, data, function(_) { 
			if (_.error) {
				ajaxError(_.msg);
				$(obj).removeAttr("disabled");
				$(obj).removeClass("isclick");
			} else {
				ajaxSuccess(_.msg, function(){
					$(obj).removeAttr("disabled");
					$(obj).removeClass("isclick");
					(typeof callback == "function") && callback(_);
				});
			}
		},'json');
	}
}

$(document).on("click", ".link-ajax", function(){
	obj = $(this);
	uri = obj.attr("data-url");
	ajaxGet(uri, obj);
});

$(document).on("click", ".link-popup", function(){
	obj = $(this);
	if(!$(this).hasClass("isclick")) {
		obj.addClass("isclick");
		title  = $(this).attr("data-title");
		uri    = $(this).attr("data-url")+"?r="+Math.random();
		width  = $(this).attr("data-width");
		height = $(this).attr("data-height");
		isFull = typeof $(this).attr("data-full") == "undefined" ? 0 : $(this).attr("data-full");
		popupShow(this, title, uri,function(){
			obj.removeClass("isclick");
		}, isFull, width, height);
	}
});

$(document).on("click", ".link-open", function(){
	obj = $(this);
	if(!$(this).hasClass("isclick")) {
		obj.addClass("isclick");
		location = $(this).attr("data-url");
	}
});

$(document).on("click", ".link-confirm", function(){
	uri = $(this).attr("data-url")+"?r="+Math.random();
	title = $(this).attr("data-confirm");
	ajaxConfirm(title,function(){
		ajaxGet(uri,$(this),function(){
			window.location.reload();
		});
	})
});

$(document).on("click", ".link-confirm-multi", function(){
	var uri = $(this).attr("data-url")+"?r="+Math.random();
	var title = $(this).attr("data-confirm");
	var tag = $(this).attr("data-tag");
	var noselect = $(this).attr("data-noselect");
	var check = $(this).attr("data-checkselect");
	
	var tagArray = tag.split(",");
	
	var data = "";
	var search = "";
	for(var x = 0; x < tagArray.length; x++) {
		var domList = $("input[name='"+tagArray[x]+"[]']");
		
		if(check == tagArray[x] && domList.length <= 0) {
			layer.msg(noselect);
			return;
		}
		if($(domList[0]).attr('type') == 'checkbox') {
			search = ":checked";
		} else {
			search = "";
		}
		domList = $("input[name='"+tagArray[x]+"[]']"+search);
		if(domList.length > 0) {
			
			var rs = [];
			for(var i = 0; i < domList.length; i++) {
				rs.push($(domList[i]).val());
			}
			data += '"'+tagArray[x] + "\":" + JSON.stringify(rs)+",";
		}
	}
	
	data = "{ " + data.substr(0, data.length-1) + "}";
	ajaxConfirm(title,function(){
		ajaxPost(uri,$(this),$.parseJSON(data),function(){
			location.reload();
		});
	})
});

$(document).on("click", ".link-prompt", function(){
	obj = $(this);
	var index = layer.prompt({
		title: '请输入：',
		value: obj.attr('data-prompt'),
		formType: 0
	}, function(text){
		typeof $(obj.attr('data-to')) == "object" && $(obj.attr('data-to')).val(text);
		typeof $(obj.attr('data-show')) == "object" && $(obj.attr('data-show')).html(text);
		typeof obj.attr('data-prompt') != "undefined" && obj.attr('data-prompt',text);
		layer.close(index);
	});
});

$(document).on("click", ".check-all", function(){
	obj = $(this);
	target = $(obj.attr("data-to"));
	target.find("input[type=checkbox]").each(function() {
		$(this).prop("checked",obj.is(":checked"));
	});
});


function getGoodsClass(dom, uri, callback) {
	var tag = true;
	var obj = $(dom);
	$.get(uri,{},function(r){
		obj.empty();
		if(r == null) return;
		$.each(r, function(key, value){
			var tmp = $("<option>");
			tmp.val(value.class_id);
			tmp.html(value.class_name);
			obj.append(tmp);
		});
		if(typeof callback == "function") {
			callback();
		}
		$(obj).trigger("change");
	},"json");
}
function getProvince(dom, uri, callback) {
	var tag = true;
	var obj = $(dom);
	$.get(uri,{},function(r){
		if(r == null) return;
		$.each(r, function(key, value){
			var tmp = $("<option>");
			tmp.val(value.provinceid);
			tmp.html(value.provincename);
			obj.append(tmp);
		});
		if(typeof callback == "function") {
			callback();
		}
		$(obj).trigger("change");
	},"json");
}
function getCitys(dom, uri, callback){
	var tag = true;
	var obj = $(dom);
	$.get(uri,{},function(r){
		if(r == null) return;
		obj.empty();
		$.each(r, function(key, value){
			var tmp = $("<option>");
			tmp.val(value.cityid);
			tmp.html(value.cityname);
			obj.append(tmp);
		});
		if(typeof callback == "function") {
			callback();
		}
		$(obj).trigger("change");
	},"json");
}
function getDistrict(dom, uri, callback){
	var tag = true;
	var obj = $(dom);
	$.get(uri,{},function(r){
		if(r == null) return;
		obj.empty();
		$.each(r, function(key, value){
			var tmp = $("<option>");
			tmp.val(value.districtid);
			tmp.html(value.districtname);
			obj.append(tmp);
		});
		if(typeof callback == "function") {
			callback();
		}
		$(obj).trigger("change");
	},"json");
}

function get_Province(dom, uri, callback) {
	var tag = true;
	var obj = $(dom);
	$.get(uri,{},function(r){
		obj.empty();
		var noSelect = $("<option>");
		noSelect.val("-1");
		noSelect.html("--请选择省份--");
		obj.append(noSelect);
		if(r == null) return;
		$.each(r, function(key, value){
			var tmp = $("<option>");
			tmp.val(value.provinceid);
			tmp.html(value.provincename);
			obj.append(tmp);
		});
		if(typeof callback == "function") {
			callback();
		}
		$(obj).trigger("change");
	},"json");
}
function get_Citys(dom, uri, callback){
	var tag = true;
	var obj = $(dom);
	$.get(uri,{},function(r){
		obj.empty();
		var noSelect = $("<option>");
		noSelect.val("-1");
		noSelect.html("--请选择城市--");
		obj.append(noSelect);
		if(r == null) return;
		$.each(r, function(key, value){
			var tmp = $("<option>");
			tmp.val(value.cityid);
			tmp.html(value.cityname);
			obj.append(tmp);
		});
		if(typeof callback == "function") {
			callback();
		}
		$(obj).trigger("change");
	},"json");
}
function get_District(dom, uri, callback){
	var tag = true;
	var obj = $(dom);
	$.get(uri,{},function(r){
		obj.empty();
		var noSelect = $("<option>");
		noSelect.val("-1");
		noSelect.html("--请选择地区--");
		obj.append(noSelect);
		if(r == null) return;
		$.each(r, function(key, value){
			var tmp = $("<option>");
			tmp.val(value.districtid);
			tmp.html(value.districtname);
			obj.append(tmp);
		});
		if(typeof callback == "function") {
			callback();
		}
		$(obj).trigger("change");
	},"json");
}

function sendFile(id, file, editor, welEditable){
	$(".note-toolbar.btn-toolbar").append('正在上传图片');
	var filename = false;
	try{
		filename = file['name'];
	} catch(e) {
		filename = false;
	}
	if(!filename){
		$(".note-alarm").remove();
	}
	//以上防止在图片在编辑器内拖拽引发第二次上传导致的提示错误
	var ext = filename.substr(filename.lastIndexOf("."));
	ext = ext.toUpperCase();
	var timestamp = new Date().getTime();
	var name = timestamp+"_"+$("#"+id).attr('aid')+ext;
	data = new FormData();
	data.append("file", file);
	data.append("key",name);
	data.append("token",$("#"+id).attr('token'));
	$.ajax({
		data: data,
		type: "POST",
		url: "/ajax.php/Common/uploadImage.html",
		cache: false,
		contentType: false,
		processData: false,
		success: function(data) {
			$("#"+id).summernote('editor.insertImage', data.files);
			$(".note-alarm").html("上传成功,请等待加载");
			setTimeout(function(){$(".note-alarm").remove();},3000);
		},
		error:function(){
			$(".note-alarm").html("上传失败");
			setTimeout(function(){$(".note-alarm").remove();},3000);
		}
	});
}

